﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Nizm0.QOT.Library.Windows
{
    public partial class frmQueryGeneratorProperties : Form
    {
        private Library.Objects.ListOfVariable _listOfVariable;

        public Library.Objects.ListOfVariable ListOfVariable
        {
            get { return _listOfVariable; }
            //set { _listOfVariable = value; }
        }

        public frmQueryGeneratorProperties(List<Library.Objects.IVariable> listofvariable)
        {
            InitializeComponent();
            _listOfVariable = new Library.Objects.ListOfVariable();
            foreach (Library.Objects.Variable tmpvar in listofvariable)
            {
                _listOfVariable.Add(tmpvar);
            }
        }
        public frmQueryGeneratorProperties(Library.Objects.ListOfVariable listofvariable)
        {
            InitializeComponent();
            _listOfVariable = listofvariable;
        }
        public frmQueryGeneratorProperties()
        {
            InitializeComponent();
        }

        public frmQueryGeneratorProperties(List<Objects.VariableInTheProcedure> list)
        {
            InitializeComponent();

            _listOfVariable = new Library.Objects.ListOfVariable();
            foreach (Library.Objects.Variable tmpvar in list)
            {
                _listOfVariable.Add(tmpvar);
            }
        }

        private void frmQueryGeneratorProperties_Load(object sender, EventArgs e)
        {
            if (_listOfVariable != null)
            {
                foreach (Library.Objects.Variable tmpvar in _listOfVariable)
                {
                    AddNewElement(tmpvar.Name, tmpvar.DataType, null);
                }
            }

            this.DataType.Items.AddRange(Library.Objects.Variable.DataTypeTab);
            //this.Generator.Items.AddRange(Library.Generators.Record.GeneratorsList);
            this.Generator.DataSource = Library.Generators.Record.GeneratorsList;
        }

        private void AddNewElement(string name, string type, string generator)
        {
            DataGridViewRow newrow = new DataGridViewRow();

            DataGridViewCell varName = new DataGridViewTextBoxCell();
            DataGridViewComboBoxCell varDataType = new DataGridViewComboBoxCell();
            DataGridViewComboBoxCell varGenerator = new DataGridViewComboBoxCell();
            DataGridViewComboBoxCell varColumn = new DataGridViewComboBoxCell();

            varName.Value = name;
            foreach (string str in Library.Objects.Variable.DataTypeTab)
            {
                varDataType.Items.Add(str);
            }
            varDataType.Value = type;

            newrow.Cells.Add(varName);
            newrow.Cells.Add(varDataType);
            newrow.Cells.Add(varGenerator);
            newrow.Cells.Add(varColumn);

            dataGridView1.Rows.Add(newrow);
        }

        private DataGridViewComboBoxCell CreateComboBoxCell(string[] strlist)
        {
            DataGridViewComboBoxCell tmpcell = new DataGridViewComboBoxCell();

            foreach (string str in strlist)
            {
                tmpcell.Items.Add(str);
            }

            return tmpcell;
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //System.Threading.Thread.Sleep(1);
            if (_listOfVariable == null)
            {
                _listOfVariable = new Library.Objects.ListOfVariable();
            }
            if (!_listOfVariable.Contains(dataGridView1.Rows[e.RowIndex].Cells["ParameterName"].Value.ToString()))
            {
                _listOfVariable.Add(new Library.Objects.Variable(dataGridView1.Rows[e.RowIndex].Cells["ParameterName"].Value.ToString(), null, null));
            }
            else
            {
                if (e.ColumnIndex == dataGridView1.Columns["DataType"].Index)
                    _listOfVariable[_listOfVariable.IndexOf(dataGridView1.Rows[e.RowIndex].Cells["ParameterName"].Value.ToString())].DataType = dataGridView1.Rows[e.RowIndex].Cells["DataType"].Value.ToString();
            }
        }

        private void frmQueryGeneratorProperties_FormClosing(object sender, FormClosingEventArgs e)
        {
            //dataGridView1.CancelEdit();
            dataGridView1.EndEdit();
            List<string> listnotremowe=new List<string>();
            foreach (DataGridViewRow tmprow in dataGridView1.Rows)
            {
                if (tmprow.Cells["ParameterName"].Value != null)// && !_listOfVariable.Contains(tmprow.Cells["ParameterName"].Value.ToString())
                {
                    listnotremowe.Add(tmprow.Cells["ParameterName"].Value.ToString());
                }
            }
            _listOfVariable.RemoveExcept(listnotremowe);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["Config"].Index)
            {

            }
        }

        protected void OpenGeneratorConfig(string generator)
        {
            switch (generator)
            {
                case "SQLScript":
                    {
                        break;
                    }
                case "Pesel":
                    {
                        break;
                    }
                default:
                    break;
            }
        }

    }
}
